﻿using System;
using Chambills.Present.Core;
using NUnit.Framework;
using Present.Commands.Async.Actions;
using Present.Commands.Async.Actions.Decorators;
using Present.Commands.Fluent;
using Rhino.Mocks;

namespace Present.Commands.Tests.Fluent
{
    [TestFixture]
    public class FluentAsyncActionApiTestCase
    {
        [Test]
        public void CompositeAction_Actions_AreAdded()
        {
            var action1 = StubObservableAction();
            var action2 = StubObservableAction();
           CompositeAsyncAction compositeAction = AsyncAction.Multiple()
                                                           .Add(action1)
                                                           .Add(action2);

            Assert.Contains(action1, compositeAction.Actions);
            Assert.Contains(action2, compositeAction.Actions);
        }

        [Test]
        public void CompositeAction_HandleError()
        {
            var action = MockRepository.GenerateStub<IObservableCommandAction>();
            Exception handled = null;
            CompositeAsyncAction compositeAction = AsyncAction.Multiple().Add(action).HandleError(e => handled = e);
            
            var exception = new Exception();
            compositeAction.HandleError(exception);
            
            Assert.AreSame(exception, handled);
        }

      

        private IObservableCommandAction StubObservableAction()
        {
            var o = MockRepository.GenerateStub<IObservableCommandAction>();
            o.Stub(x => x.Subscribe(null)).IgnoreArguments().Return(new DisposableAction(() => { }));
            return o;
        }
    }
}
